package com.ghy.utils.File;


import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 文件处理类
 */
public class fileUpload {

    /***
     * 存放照片返回路径
     * */
    public static String img(String funcld, MultipartFile mFile, String serverPath) {

        //1.1获得文件名
        String frontPhotoName = mFile.getOriginalFilename();
        byte[] fileBytes;
        File dest;//目标文件（上传以后存储的文件）
        String fileDigest;
        String src;//返回的路径
        try {
            //1.2获得文件字节
            fileBytes = mFile.getBytes();
            fileDigest = DigestUtils.md5Hex(fileBytes);

            //1.3执行上传动作
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            String dateStr = sdf.format(new Date());
            String newFileName =
                    UUID.randomUUID().toString() + "." +
                            //获得文件名字扩展名
                            FilenameUtils.getExtension(frontPhotoName);//文件扩展名

            String realPath = serverPath + "/resources/static/" + funcld + "/" + dateStr + newFileName;

            src = "/Path/" + funcld + "/" + dateStr + newFileName;
            dest = new File(realPath);
            File parent = dest.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();

            }

            mFile.transferTo(dest);//上传
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("upload error");
        }

        return src;
    }

    /**
     * 根据url获取文件绝对物理路径
     * author: yuyy
     * date: 2022/4/26 14:17
     * @param url 数据库中保存的路径
     * @param serverPath 根目录 可以为空
     * @return List<String>
     **/
    public static List<String> get_FileRealPath(String url,String serverPath){
        if (StringUtils.isEmpty(serverPath))
        {
            File file = new File(InterceptorConfig.basePath);
            serverPath = file.getAbsolutePath();
        }
        List<String> dst = new ArrayList<>();
        if (StringUtils.isEmpty(url))
            return dst;

        String[] ss = url.split(",");
        for (String path:
                ss) {
            String realPath = path.replace("/Path/","/resources/static/");
            dst.add(serverPath + realPath);
        }

        return dst;
    }

    /**
     *
     * author: yuyy
     * date: 2022/4/26 14:44
     * @param funcld
     * @param serverPath
     * @param fileName
     * @return Map realPath 和 urlPath
     **/
    public static Map<String,String> getFilePath(String funcld, String serverPath, String fileName){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String dateStr = sdf.format(new Date());
        String dstFilePath = funcld + "/" + dateStr + fileName;
        String realPath = serverPath + "/resources/static/" + dstFilePath;
        String urlPath = "/Path/" + dstFilePath;
        Map<String,String> map = new HashMap<>();
        map.put("realPath",realPath);
        map.put("urlPath",urlPath);
        return map;

    }

    /**
     * 根据数据量里面的路径删除文件
     * author: yuyy
     * date: 2022/4/26 14:49
     * @param urlPath 数据库里面存放的文件路径
     * @param serverPath 服务器路径
     * @return boolean
     **/
    public static boolean deleteFile_From_urlPath(String urlPath,String serverPath){
        List<String> realPaths = get_FileRealPath(urlPath,serverPath);
        File f = null;
        for (String path:realPaths) {
            f = new File(path);
            if (f.exists())
                f.delete();
        }
        return true;
    }
}